﻿Namespace Catalog
    Public Class Pack
        Inherits BaseObject
        Public Property SKU As String
        Public Property PublishDate As Date
        Public Property Description As String
        Public Property Active As Boolean
        Public Property PartCount As Integer
        Public Property Exclusive As Boolean
        Public Property AudioTypes As List(Of BaseObject)
        Public Property Genres As List(Of Genre)
        Public Property Artists As List(Of Artist)
        Public Property Demos As List(Of Demo)
        Public Property Formats As List(Of Format)
        Public Property Label As Label
        Public Property Images As List(Of Image)

        Public Sub New()
            Me.AudioTypes = New List(Of BaseObject)
            Me.Genres = New List(Of Genre)()
            Me.Artists = New List(Of Artist)
            Me.Demos = New List(Of Demo)
            Me.Formats = New List(Of Format)
            Me.Images = New List(Of Image)
        End Sub

        Public Shared Function FromBPObject(pac As BPOPack) As Pack
            If pac Is Nothing Then Return Nothing
            Dim newPack As New Pack()

            With newPack
                .SKU = pac.sku
                Date.TryParseExact(pac.publishDate, "yyyy-MM-dd", Nothing, Globalization.DateTimeStyles.None, newPack.PublishDate)
                .Description = pac.description
                .Active = pac.active
                .PartCount = pac.partCount
                .Exclusive = pac.exclusive

                If pac.audioTypes IsNot Nothing Then
                    For Each at In pac.audioTypes
                        .AudioTypes.Add(New BaseObject(at.id, at.name, at.slug))
                    Next
                End If

                If pac.genres IsNot Nothing Then
                    For Each gen As BPOGenreBase In pac.genres
                        newPack.Genres.Add(New Genre(gen.id, gen.name, gen.slug))
                    Next
                End If

                If pac.artists IsNot Nothing Then
                    For Each art As BPOArtistBase In pac.artists
                        .Artists.Add(Artist.FromBPObject(art))
                    Next
                End If

                If pac.demos IsNot Nothing Then
                    For Each dem As BPODemo In pac.demos
                        .Demos.Add(Demo.FromBPObject(dem))
                    Next
                End If

                If pac.formats IsNot Nothing Then
                    For Each frmt As KeyValuePair(Of String, BPOFormat) In pac.formats
                        .Formats.Add(Format.FromBPObject(frmt))
                    Next
                End If

                If pac.label IsNot Nothing Then
                    .Label = New Label(pac.label.id, pac.label.name, pac.label.slug)
                End If

                If pac.images IsNot Nothing Then
                    For Each img As KeyValuePair(Of String, BPOImage) In pac.images
                        .Images.Add(Image.FromBPObject(img))
                    Next
                End If
            End With

            Return newPack
        End Function
    End Class
End Namespace